<?php

	/**
	 * Crawls product item from YandexMarket
	 *
	 * @author anton.perkin
	 * @version 1.0
	 *
	 */
	class YandexMarket implements ProductSource
	{
		/* VARIABLES */
		
		/**
		 * Product list base URL (popular)
		 *
		 * TVs: http://market.yandex.ru/guru.xml?CMD=-RR=9,0,0,0-VIS=211E2-CAT_ID=108206-EXC=1-PG=*&hid=90639&filter=&num=&greed_mode=false
		 *
		 * @var string
		 */
		public $listPopularUrl = 'http://market.yandex.ru/guru.xml?CMD=-RR=9,0,0,0-VIS=960-CAT_ID=100514-EXC=1-PG=*&hid=91148';

		/**
		 * Product list base URL (recent)
		 * 
		 * TVs: http://market.yandex.ru/guru.xml?CMD=-RR=9,0,0,0-VIS=19E2-CAT_ID=108206-EXC=1-PG=10&hid=90639&filter=&num=&greed_mode=false
		 *
		 * @var string
		 */
		public $listRecentUrl = 'http://market.yandex.ru/guru.xml?CMD=-RR=9,0,0,0-VIS=160-CAT_ID=100514-EXC=1-PG=*&hid=91148';
		
		/**
		 * Product item base URL
		 *
		 * TVs: http://market.yandex.ru/model.xml?modelid=*&hid=90639
		 *
		 * @var string
		 */
		public $itemUrl = 'http://market.yandex.ru/model.xml?modelid=*&hid=91148';
		
		/**
		 * Product URLs
		 *
		 * @var array
		 */
		public $productURLs;
		
		
		/* CONSTRUCTORS */
			
		/**
		 * Defualt constructor
		 *
		 */
		public function YandexMarket()
		{
			
		}		
		
		
		/* METHODS */			
				
		/**
		 * Gets product URLs list
		 *
		 * @param int $fromPage from page
		 * @param int $toPage to page
		 */
		public function GetURLs($fromPage = '', $toPage = '', $url, $sort)
		{
			$urls = array();
//			$listURL = ($type == 'recent') ? $this->listRecentUrl : $this->listPopularUrl;
			$listURL = str_replace('*sort*', $sort, $url);
			
			for ($i = $fromPage; $i <= $toPage; $i++)
			{
				//parsed page
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, str_replace('*page*', ($i - 1)*10, $listURL));
//				curl_setopt($ch, CURLOPT_COOKIEJAR, "/cookie.txt");
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				$contents = curl_exec($ch);
				curl_close ($ch);

//				$ch = curl_init();
//				curl_setopt($ch, CURLOPT_URL, str_replace('*page*', ($i - 1)*10, $listURL));
//				curl_setopt($ch, CURLOPT_COOKIEFILE, "/cookie.txt");
//				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//				$contents = curl_exec($ch);
//				curl_close ($ch);
				
				$page = phpQuery::newDocumentHTML($contents);
				
//				$page = phpQuery::newDocumentFileHTML(str_replace('*page*', ($i - 1)*10, $listURL));
//				Utils::VarDump(str_replace('*page*', ($i - 1)*10, $listURL));
				
				$links = $page->find("a.b-offers__name");
				if (count($links) != 0)
				{
					foreach ($links as $link)
					{
						$urls[] = 'http://market.yandex.ru/'.pq($link)->attr('href');
					}
				}
				else 
				{
					echo 'Banned!';
					return $urls;
				}
			}
			
			return $urls;
		}						
		
		/**
		 * Gets product item
		 * 
		 * @param string $url product url
		 * 
		 * @return ProductItem
		 *
		 */
		public function GetProductItem($url)
		{
			//product item
			$productItem = new ProductItem();
			
			//id
			$parsedURL = parse_url($url);
			parse_str($parsedURL['query'], $urlParams);
			$productItem->id = $urlParams['modelid'];			
			
			//parsed page
			$page = phpQuery::newDocumentFileHTML($url);
			$specsPage = phpQuery::newDocumentFileHTML(str_replace('model.xml', 'model-spec.xml', $url));
//			$page = phpQuery::newDocumentFileHTML($_SERVER['DOCUMENT_ROOT']."/yandex/results/products/{$productItem->id}.html");
//			$specsPage = phpQuery::newDocumentFileHTML($_SERVER['DOCUMENT_ROOT']."/yandex/results/products/{$productItem->id}_specs.html");
			
			//name
			$productItem->name = preg_replace('~<span [\s\S]*?</span>~', '', trim($page->find(".b-page-title.b-page-title_type_model")->html()));
			
			//images
			$productItem->images = $this->GetProductImages($page, $productItem);
			
			//item url
			$productItem->url = $url;
			
			//HTML page content
			$productItem->modelPage = $page;
			$productItem->specsPage = $specsPage;
			
			return $productItem;
		}
		
		/**
		 * Gets product details
		 * 
		 * @param int $id yandex product id
		 * 
		 * @return ProductItem
		 *
		 */
		public function GetProductDetails($product)
		{
			//parsed page
			$id = $product['product_public_id'];
			$page = phpQuery::newDocumentFileHTML($_SERVER['DOCUMENT_ROOT']."/yandex/results/products/$id.html");
			$specsPage = phpQuery::newDocumentFileHTML($_SERVER['DOCUMENT_ROOT']."/yandex/results/products/{$id}_specs.html");
			
			//product item
			$productItem = new ProductItem();	

			//data from product
			$productItem->id = $product['product_public_id'];
			$productItem->internalId = $product['product_id'];
			$productItem->categoryId = $product['category_id'];
			$productItem->name = $product['product_name'];
			$productItem->url = $product['product_url'];
			$productItem->urlId = $product['product_url_id'];
			$productItem->brand = $specsPage->find(".b-breadcrumbs__link:last")->html();
			
			//crawl all attributes + attribute categories
			$dataHTML = $specsPage->find(".b-properties tr");
			foreach ($dataHTML as $data)
			{
				$sectionTitleTemp = pq($data)->find("th.b-properties__title")->html();
				if (empty($sectionTitleTemp))
				{
					$label = pq($data)->find(".b-properties__label.b-properties__label-title span")->html();
					$value = pq($data)->find(".b-properties__value")->html();
					$detailItem = array('label' => $label, 'value' => $value);
					$detailsData[$sectionTitle][] = $detailItem;
				}
				else 
				{
					$sectionTitle = $sectionTitleTemp;
				}
			}
			$productItem->attributes = $detailsData;
			
			return $productItem;
		}		
		
		/**
		 * Saves product URLs
		 * 
		 * @return array
		 *
		 */
		public function SaveURLs($urls, $category, $source, $sortType)
		{
			$result = array('success' => 0, 'fail' => 0);
			
			foreach ($urls as $url)
			{
				$productItem = new ProductItem();
				$saveResult = $productItem->SaveURL($url, $category, $source, $sortType);
				if ($saveResult)
				{
					$result['success']++;
				}
				else 
				{
					$result['fail']++;
				}
			}
			
			return $result;
		}

		/**
		 * Gets  products URLs list
		 *
		 * @param int $fromRecord offset
		 * @param int $quantity count
		 */
		public function GetSavedURLs($fromRecord = '', $quantity = '')
		{
			//limit
			if (($fromRecord == '') && ($quantity == ''))
			{
				$limit = '';
			}
			else 
			{
				$fromRecord = ($fromRecord == '') ? 0 : $fromRecord;
				$quantity = ($quantity == '') ? 1000000 : $quantity;
				$limit = "LIMIT $fromRecord, $quantity";
			}

			$urls = DB::ListItems("
				SELECT * FROM t_product_urls WHERE product_url_source = 'yandex' $limit;
			");
			
			return $urls;
		}		

		/**
		 * Gets saved products list
		 *
		 * @param int $fromRecord offset
		 * @param int $quantity count
		 */
		public function GetSavedProducts($fromRecord = '', $quantity = '')
		{
			//limit
			if (($fromRecord == '') && ($quantity == ''))
			{
				$limit = '';
			}
			else 
			{
				$fromRecord = ($fromRecord == '') ? 0 : $fromRecord;
				$quantity = ($quantity == '') ? 1000000 : $quantity;
				$limit = "LIMIT $fromRecord, $quantity";
			}

			$products = DB::ListItems("
				SELECT * FROM t_products $limit;
			");
			
			return $products;
		}	

		/**
		 * Gets product images and downloads them
		 *
		 * @param object $page PhpQuery object
		 * @param object ProductItem $productItem product
		 * 
		 * @return array
		 * 
		 */
		private function GetProductImages($page, $productItem)
		{
			//get images' URLs
			$images[0] = $page->find('.b-model-pictures__big > a')->attr('href');
			$otherImages = $page->find('.b-model-pictures__small > a');
			if (count($otherImages) > 0)
			{
				foreach ($otherImages as $otherImage)
				{
					$href = pq($otherImage)->attr('href');
					if (!empty($href))
					{
						$images[] = $href;
					}
				}
			}
			
			//copy images
			foreach ($images as $key => $image)
			{
				$imageInfo = pathinfo($image);
				if (!isset($imageInfo['extension'])) continue;
				if (!file_exists($_SERVER['DOCUMENT_ROOT']."/yandex/results/images/{$productItem->id}/"))
				{
					mkdir($_SERVER['DOCUMENT_ROOT']."/yandex/results/images/{$productItem->id}/");
				}
				copy($image, $_SERVER['DOCUMENT_ROOT']."/yandex/results/images/{$productItem->id}/{$key}.{$imageInfo['extension']}");
			}
			
			return $images;
		}
		
	}
?>